﻿using Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinCourseControls.DAL;
using WinCourseControls.Models;

namespace WinCourseControls
{
    public partial class FrmDataGridView2 : Form
    {
        public FrmDataGridView2()
        {
            InitializeComponent();
        }
        StudentDAL stuDAL = new StudentDAL();
        ClassDAL classDAL = new ClassDAL();
        private void FrmDataGridView2_Load(object sender, EventArgs e)
        {
            //下拉框列数据源
            List<ClassInfo> classList = classDAL.GetModelList("ClassId,ClassName", "", 0);
            DataGridViewComboBoxColumn cboCol = dgvStudents.Columns["colClassId"] as DataGridViewComboBoxColumn;
            cboCol.DataSource = classList;
            cboCol.DisplayMember = "ClassName";
            cboCol.ValueMember = "ClassId";

            //加载学生数据   DataTable
            LoadStudents();
        }

        /// <summary>
        /// 刷新列表数据
        /// </summary>
        private void LoadStudents()
        {
            string sql = "select StuId,StuName,ClassId,Sex,Phone from StudentInfos where IsDeleted=0";
            DataTable dt = stuDAL.GetDt(sql, 1);
            dgvStudents.AutoGenerateColumns = false;  // 不自动生成列标题
            dgvStudents.DataSource = dt;
        }

        /// <summary>
        /// 新增行按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStudent_Click(object sender, EventArgs e)
        {
            DataTable dt = dgvStudents.DataSource as DataTable;
            DataRow dr=  dt.Rows.Add();
            dr["Sex"] = "男";
        }

        /// <summary>
        /// 确定按钮-提交列表修改的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            DataTable dt = dgvStudents.DataSource as DataTable;
            //提交。。。。
            //Adapter提交DataTable中更新   Adapter.Update(dt);
            SqlHelper.AdapterUpdateDt(dt, "select StuId, StuName, ClassId, Sex, Phone from StudentInfos");
            MessageBox.Show("提交成功！");
            LoadStudents();
        }
    }
}
